package cn.community.care.module.system.service.inventoryinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.module.system.controller.admin.inventoryinfo.vo.*;
import cn.community.care.module.system.dal.dataobject.inventoryinfo.InventoryInfoDO;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;

import cn.community.care.module.system.dal.mysql.inventoryinfo.InventoryInfoMapper;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.community.care.module.system.enums.ErrorCodeConstants.*;

/**
 * 库存信息 Service 实现类
 *
 * @author 美妆管理员
 */
@Service
@Validated
public class InventoryInfoServiceImpl implements InventoryInfoService {

    @Resource
    private InventoryInfoMapper inventoryInfoMapper;

    @Override
    public Long createInventoryInfo(InventoryInfoSaveReqVO createReqVO) {
        // 插入
        InventoryInfoDO inventoryInfo = BeanUtils.toBean(createReqVO, InventoryInfoDO.class);
        inventoryInfoMapper.insert(inventoryInfo);
        // 返回
        return inventoryInfo.getId();
    }

    @Override
    public void updateInventoryInfo(InventoryInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateInventoryInfoExists(updateReqVO.getId());
        // 更新
        InventoryInfoDO updateObj = BeanUtils.toBean(updateReqVO, InventoryInfoDO.class);
        inventoryInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteInventoryInfo(Long id) {
        // 校验存在
        validateInventoryInfoExists(id);
        // 删除
        inventoryInfoMapper.deleteById(id);
    }

    private void validateInventoryInfoExists(Long id) {
        if (inventoryInfoMapper.selectById(id) == null) {
//            throw exception(INVENTORY_INFO_NOT_EXISTS);
        }
    }

    @Override
    public InventoryInfoDO getInventoryInfo(Long id) {
        return inventoryInfoMapper.selectById(id);
    }

    @Override
    public PageResult<InventoryInfoDO> getInventoryInfoPage(InventoryInfoPageReqVO pageReqVO) {
        return inventoryInfoMapper.selectPage(pageReqVO);
    }

}